/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sd_proj1.mainProgram;

import sd_proj1.monSolution.FifoNode;
import sd_proj1.interfaces.*;

/**
 *
 * @author Pedro Costa Correia - 32795
 * @author Valter Pedro Lopes Vicente - 39360
 */
public class Owner extends Thread {

	/**
	 *  Instanciação da interface da dona implementada no monitor da loja.
	 *
	 *    @serialField store
	 */
	private IOwnerStore store;
	/**
	 *  Instanciação da interface da dona implementada no monitor da oficina.
	 *
	 *    @serialField workshop
	 */
	private IOwnerWorkshop workshop;
	/**
	 *  Instanciação da interface da dona implementada no estado geral.
	 *
	 *    @serialField general
	 */
	private IOwnerFifo general;
	private FifoNode obj;

	/**
	 *  Instanciação da thread da dona.
	 *
	 * @param store Interface da dona associada ao monitor da loja.
	 * @param workshop Interface da dona associada ao monitor da oficina.
	 * @param general Interface da dona associada ao estado geral.
	 */
	public Owner(IOwnerStore store, IOwnerWorkshop workshop, IOwnerFifo general) {
		this.store = store;
		this.workshop = workshop;
		this.general = general;
	}

	/**
	 * Ciclo de vida da thread da dona.
	 */
	@Override
	public void run() {
		int c;									 /* customer id */
		char nextTask = '0';					  /* task to be carried out */
		boolean busy;								 /* in-the-shop signaling flag TRUE if the entrepreneur is in the shop */
		while (!store.endOperEntrep()) {
			store.prepareToWork();				    /* the entrepreneur opens the shop and gets ready to perform her duties */
			busy = (true);						       /* set busy flag */
			while (busy) {
				obj = general.readRequest();
				nextTask = obj.appraiseSit();
				if (nextTask == 'C') {					/* the entrepreneur checks if a customer is needing attention */
					c = obj.addressACustomer();	/* the entrepreneur goes to the counter to attend a customer */
					serviceCustomer();				  /* the entrepreneur services the customer */
					store.sayGoodByeToCustomer(c);          /* the entrepreneur completes the transaction */
				} else {
					busy = (false);                            /* reset busy flag */
					/* the entrepreneur checks if some craftsman has phoned him */
					if ((nextTask == 'G') || (nextTask == 'P')) {
						if (store.customersInTheShop()) {
							store.closeTheDoor();		  /* the entrepreneur closes the door to prevent further customers to enter */
							busy = (true);				  /* set busy flag */
						}
					}
				}
			}
			store.prepareToLeave();                    /* the entrepreneur closes the shop */
			if (nextTask == 'G') {
				workshop.goToWorkshop();         /* the entrepreneur collects a batch of goods */
			} else if (nextTask == 'P') {
				workshop.visitSuppliers(); /* the entrepreneur goes shopping for prime materials and delivers them to the workshop */
			}

			store.returnToShop();                /* the entrepreneur goes back to the shop */
		}

	}

	/**
	 * A dona serve o cliente (operação interna).
	 */
	private void serviceCustomer() {
		try {
			sleep((long) (5 + 10 * Math.random()));
		} catch (InterruptedException e) {
		}
	}
}

